package br.com.linkers.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;

import br.com.linkers.data.HibernateUtil;
import br.com.linkers.entity.Enderecos;

public class EnderecoDAO {

	Session session = HibernateUtil.getSessionFactory().openSession();
	Transaction transaction = session.beginTransaction();

	List<Enderecos> lista;

	public void newSession() {
		this.session = HibernateUtil.getSessionFactory().openSession();
		this.transaction = session.beginTransaction();
	}

	public void inserir(Enderecos endereco) {

		newSession();

		session.persist(endereco);

		transaction.commit();
		session.close();
	}

	public void deletar(Enderecos endereco) {

		newSession();

		session.delete(endereco);

		transaction.commit();
		session.close();
	}

	public void atualizar(Enderecos endereco) {
		session.update(endereco);
	}

	// achei na internet coloquei aqui pra ver alguem entende
	public Enderecos buscarPorId(Long id) {
		return (Enderecos) session.load(Enderecos.class, id);
	}

	public List<Enderecos> listar() {
		newSession();
		Criteria cri = session.createCriteria(Enderecos.class);
		this.lista = cri.list();
		return this.lista;
	}
}
